package com.twu.biblioteca;


import com.twu.driver.Driver;
import com.twu.user.User;
import com.twu.user.UserDatabase;
import com.twu.commandSetup.LibraryOperation;
import com.twu.io.IOHandler;

//Display Menu
//Get user input
//User Login
//Checkout Movies
//Return Movies
//Checkout Book
//Return Book



public class BibliotecaApp {

    private Driver driver;
    private String welcomeMessage = "Welcome to Biblioteca!";
    UserDatabase userDatabase = new UserDatabase();
    int loggedInUser;
    private IOHandler io = new IOHandler();
    private Library library = new Library();

    public BibliotecaApp() {

        for (int i = 1; i < 10; i++) {
            library.addNewBook("Book" + i, "Author" + i, "Pub" + i, "2000");
            library.addNewMovie("Movie" + i, "Pro" + i, "Dir" + i, "2000");
            userDatabase.addCustomer("user" + i, "pass" + i);
        }
        userDatabase.addAdmin("admin","admin");
        driver =  new Driver(library,userDatabase);
    }

    public static void main(String[] args) {
        new BibliotecaApp().start();
    }

    private void start() {
        io.displayString(welcomeMessage);
        if (!userLogin()) return;
        if(userDatabase.getUserById(loggedInUser).getType() == User.userType.CUSTOMER)
            new BibliotecaApp().userModule();
        else
            new BibliotecaApp().adminModule();
    }

    private void adminModule() {
        library.displayCheckoutUserList(userDatabase);

    }

    private void userModule() {
        int choice;
        do
        {
            displayChoices();
            choice = io.getInteger();
            LibraryOperation.values()[choice].execute(driver);

        }while(choice != LibraryOperation.EXIT.ordinal());

        return;
    }

    private boolean userLogin() {
        io.displayString("Enter Username : ");
        String username = io.getString();
        io.displayString("Enter Password : ");
        String password = io.getString();
        if((loggedInUser = userDatabase.login(username, password)) == 0)
        {
            io.displayString("User login failed!");
            return false;
        }
        library.setLoggedInUser(loggedInUser);
        return true;
    }

    private void displayChoices() {
        io.displayString("Select Option :");
        int i;
        for(LibraryOperation l :LibraryOperation.values())
        {
            System.out.println(l.ordinal() + ". " + l.getOperationString());
        }
    }
}
